package com.mifengs.mall.ui.main;

import android.text.TextUtils;

import com.mifengs.commonbase.util.PreferenceUtils;
import com.mifengs.mall.Constant;
import com.mifengs.mall.MallApplication;
import com.mifengs.mall.entity.HomeData;
import com.mifengs.mall.entity.ResponseBean;
import com.mifengs.mall.entity.ResponseInfo;
import com.mifengs.mall.entity.StoreApplyInfo;
import com.mifengs.mall.netWork.NetWork;

import java.util.HashMap;
import java.util.Map;

import rx.Observer;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by lihaoqi on 2017/10/30.
 */

public class HomePresenter implements HomeContract.Presenter {

    private HomeContract.View mView;

    public HomePresenter(HomeContract.View mView) {
        this.mView = mView;
    }

    public static final String TAG = "HomePresenter";


    @Override
    public void getData(final int type) {
        if(type!=HomeFragment.REFRESH_DATA){
            mView.showProgress();
            try {
                HomeData homeData = MallApplication.getInstance().getCacheHelper().getAsSerializable(TAG);
                if(homeData!=null){
                    setData(homeData);
                }
            }catch (Exception e){

            }

        }
        NetWork.getHomeDataApi().getData()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(mView.<ResponseInfo<HomeData>>bindToLife())
                .subscribe(new Observer<ResponseInfo<HomeData>>() {
                    @Override
                    public void onCompleted() {}

                    @Override
                    public void onError(Throwable e) {
                        mView.showNetError();
                        if(type==HomeFragment.REFRESH_DATA){
                            mView.stopRefreshView();
                        }else{
                            mView.hideProgress();
                        }
                    }

                    @Override
                    public void onNext(ResponseInfo<HomeData> homeDataResponseInfo) {
                        mView.hideProgress();
                        mView.hideNetError();
                        if(homeDataResponseInfo.getStatus()== Constant.OK_CODE){
                            //mView.setData(homeDataResponseInfo.getResult());
                            if(type==HomeFragment.REFRESH_DATA){
                                mView.stopRefreshView();
                            }
                            HomeData data = homeDataResponseInfo.getResult();
                            if(data!=null){
                                try {
                                    MallApplication.getInstance().getCacheHelper().put(TAG,data);
                                }catch (Exception e){

                                }
                                setData(data);
                            }
                        }else{
                            if(type==HomeFragment.REFRESH_DATA){
                                mView.stopRefreshView();
                            }
                            mView.showShortToast(homeDataResponseInfo.getMsg());
                        }
                    }
                });
    }

    @Override
    public void getApplyProgress() {
        mView.showProgress();
        HashMap<String,String> hashMap = new HashMap<>();
        if(!TextUtils.isEmpty(PreferenceUtils.getToken())){
            hashMap.put("app_token",PreferenceUtils.getToken());
        }else{
            hashMap.put("app_token","");
        };
        NetWork.getHomeDataApi().getApplyProgress(hashMap)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(mView.<ResponseBean<StoreApplyInfo>>bindToLife())
                .subscribe(new Observer<ResponseBean<StoreApplyInfo>>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.hideProgress();
                        mView.showLongToast(e.toString());
                    }

                    @Override
                    public void onNext(ResponseBean<StoreApplyInfo> storeApplyInfoResponseBean) {
                        mView.hideProgress();
                        String status = storeApplyInfoResponseBean.getStatus();
                        if(Constant.OK_STATUS.equals(status)){
                            StoreApplyInfo info = storeApplyInfoResponseBean.getParamer();
                            int storeStatus = info.getStoreApplyInfoDto().getStoreStatus();
                            int isExist = info.getStoreApplyInfoDto().getIsExist(); // 0 为不存在 1 为存在
                            int type = info.getStoreApplyInfoDto().getType(); //0 个体 1 企业
                            if(isExist==0){
                                mView.startShopTypeActivity();
                            }else if (isExist==1){
//                                switch (storeStatus){
//                                    case -1:
//
//                                        break;
//                                    case 1:
//
//                                        break;
//                                    case 5:
//
//                                        break;
//                                }
                                mView.startOpenShopActivity(type,storeStatus,info);
                            }
                       }else if ("-1".equals(status)){
                            mView.startLoginActivity();
                       }
                    }
                });
    }

    @Override
    public void getPhotoPath() {
        NetWork.getHomeDataApi().getPhotoPath()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(mView.<ResponseInfo<String>>bindToLife())
                .subscribe(new Subscriber<ResponseInfo<String>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                    }

                    @Override
                    public void onNext(ResponseInfo<String> stringResponseInfo) {
                        if(stringResponseInfo.getStatus()==Constant.OK_CODE){
                            PreferenceUtils.savePhotoPath(mView.getContext(),stringResponseInfo.getResult());
                        }
                    }
                });
    }

    private void setData(HomeData data) {
        mView.setTopBanner(data.getSliders());
        mView.setMenu(data.getMenus());
        Map<String,Object> datas = new HashMap<>();
        datas.put("notices",data.getNotices());
        datas.put("topGoods",data.getScareBuying().getTopGoods());
        datas.put("moreNoticeUrl",data.getMoreNoticeUrl());
        datas.put("qgUrl",data.getScareBuying().getActionUrl());
        datas.put("bgImg",data.getScareBuying().getBgImg());
        PreferenceUtils.savePhotoUrl(MallApplication.getInstance(),data.getPhotopath());
        mView.setMidViewData(datas);
        mView.setFloors(data.getFloors());
        PreferenceUtils.saveSearchUrl(mView.getContext(),data.getSearchUrl());
    }

}
